﻿using System;
using System.Data;
using System.Windows.Forms;
using Saturn.App.FormOper;
using Saturn.App.Common;
using Saturn.App.Properties;
using Saturn.App;

namespace Saturn.App
{
    public partial class FormBy : Form
    {
        int pageIndex = 1;
        int count = 0;
        int pageSize = 6;
        int pageCount = 1;

        string billcode = "";
        //string productline = CommonCfg.ProductLine;
        string productline = "";
        string equipmentcode = "";
        string equipmentname = "";
        string maintainproject = "";
        string maintainpcontent = "";
        string starttime = "";
        string endtime = "";
        string username = "";
        string daytime = "";
        string maintaintype = "";
        SaturnWebReference.SaturnWebService soap = new Saturn.App.SaturnWebReference.SaturnWebService();
        public FormBy()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //billcode = lbl_billcode.Text;
            username = CommonCfg.Userrealname;
            productline = CommonCfg.ProductLine;
            maintainproject = cmb_equipmentproject.Text;
            maintaintype = cmb_equipmentcategory.Text;
            endtime = DateTime.Now.ToString();

            if (lbx_selected.Items.Count > 0)
            {
                maintainpcontent = "";
                for (int i = 0; i < lbx_selected.Items.Count; i++)
                {
                    DataRowView item = (DataRowView)this.lbx_selected.Items[i];
                    maintainpcontent = maintainpcontent + "," + item.Row.ItemArray[1].ToString();
                }
                maintainpcontent = maintainpcontent.Substring(1);
               
            }
            else
            {
                MessageBox.Show("未选择保养项目");
            }
            if (!String.IsNullOrEmpty(billcode)& !String.IsNullOrEmpty(productline)& !String.IsNullOrEmpty(equipmentcode)& !String.IsNullOrEmpty(equipmentname)& !String.IsNullOrEmpty(maintainproject)& !String.IsNullOrEmpty(maintainpcontent)& !String.IsNullOrEmpty(starttime)& !String.IsNullOrEmpty(endtime)& !String.IsNullOrEmpty(username)& !String.IsNullOrEmpty(maintaintype))
            {
                //调用接口保存保养数据
                bool res = soap.AddMaintainDetail(billcode, productline, equipmentcode, equipmentname, maintainproject, maintainpcontent, starttime, endtime, username, maintaintype);
                if (res)
                {
                    MessageBox.Show("保养记录已提交");
                }
                else
                {
                    MessageBox.Show("保养记录提交失败，请检查网络");
                }
            }
            else
            {
                MessageBox.Show("请完善设备保养信息");
            }
        }

        private void FormInit()
        {
            txt_equipmentcode.Enabled = true;
            cmb_equipmentproject.Enabled = true;
            cmb_equipmentcategory.Enabled = true;
            lbx_selected.Enabled = true;
            lbx_source.Enabled = true;
            //btnstart.Enabled = true;
            //btnfinish.Enabled = true;
        }
        private void btnPre_Click(object sender, EventArgs e)
        {
            btnfinish.Enabled = true;
            #region 记录开始保养时的时间
            starttime = DateTime.Now.ToString();
            daytime = DateTime.Now.ToString("yyyyMMdd");
            //生成保养单号 设备编号+日期+类型+序列号
            billcode = CommonCfg.CreateBillCode(daytime, equipmentcode, EnumClass.EquipmentType.By);
            lbl_billcode.Text = billcode; 
            #endregion
        }
        private void FrmBackWarehouse_Load(object sender, EventArgs e)
        {
        }
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyValue == (char)13 && !string.IsNullOrEmpty(e.ToString()))
            if (e.KeyValue == (char)13)
            {

                FormInit();

                DataSet ds = new DataSet();
                ds = soap.GetEquipmentDetail(txt_equipmentcode.Text.ToString());
                //设备名称
                equipmentname = ds.Tables[0].Rows[0].ItemArray[3].ToString();
                txt_equipmentname.Text = equipmentname;
                //设备名称
                equipmentcode = txt_equipmentcode.Text;
                //保养项目
                cmb_equipmentproject.DataSource = soap.GetMaintainProject().Tables[0];
                cmb_equipmentproject.ValueMember = "id";
                cmb_equipmentproject.DisplayMember = "MaintainProject";
                //保养内容
                lbx_source.DataSource = soap.GetMaintainContent().Tables[0];
                lbx_source.DisplayMember = "MaintainContent";
                lbx_source.ValueMember = "id";

                lbx_selected.DisplayMember = "MaintainContent";
                lbx_selected.ValueMember = "id";

            }
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            if (this.lbx_source.SelectedIndex >= 0)
            {
                if (!this.lbx_selected.Items.Contains(this.lbx_source.SelectedItem))
                {
                    this.lbx_selected.Items.Add(this.lbx_source.SelectedItem);
                }
                else
                {
                    MessageBox.Show("无法重复添加");
                }
            }
            else
            {
                MessageBox.Show("选择要添加的保养内容");
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (this.lbx_selected.SelectedIndex >= 0)
            {
                this.lbx_selected.Items.Remove(this.lbx_selected.SelectedItem);
            }
            else
            {
                MessageBox.Show("请选择要删除的保养项目");
            }
        }

        private void lbx_source_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}